//
//  WowAppSwitchStrategy.m
//  PersonalOCTest
//
//  Created by zlf on 2025/8/20.
//

#import "WowAppSwitchStrategy.h"

@implementation WowAppSwitchStrategy

- (BOOL)canHandleTag:(WowNotificationSwitchTag)tag {
    return tag == WowNotificationSwitchTagPhone ||
           tag == WowNotificationSwitchTagPhoneScreen ||
           tag == WowNotificationSwitchTagMessage ||
           tag == WowNotificationSwitchTagMessageScreen ||
           tag == WowNotificationSwitchTagSchedule ||
           tag == WowNotificationSwitchTagScheduleScreen ||
           tag == WowNotificationSwitchTagAlarm ||
           tag == WowNotificationSwitchTagAlarmScreen;
}

- (void)handleSwitchWithContext:(WowSwitchContext *)context {
    UISwitch *sender = context.sender;
    WowNotifiSetToDeviceManager *manager = context.manager;
    
    // 1. 设备连接检查
//    if (![manager handleSwitchAction:sender]) {
//        return;
//    }
    
    // 2. 判断操作类型
    BOOL isMainSwitch = [self isMainSwitchTag:sender.tag];
    BOOL isScreenSwitch = [self isScreenSwitchTag:sender.tag];
    
    // 3. 更新本地状态
    if (isMainSwitch) {
//        context.infoModel.setting.syncEnabled = sender.isOn;
    } else if (isScreenSwitch) {
//        context.infoModel.setting.autoScreenOn = sender.isOn;
    }
    
    // 4. 发送设备命令
    if (isMainSwitch) {
//        [manager appSet:YES
//                    row:context.row
//            syncEnabled:sender.isOn ? @YES : @NO
//               callback:^(NSDictionary *param) {
//            [self handleCallbackResult:param context:context];
//        }];
//        
//        // 电话特殊处理
//        if ([self isPhoneTag:sender.tag]) {
//            BOOL phoneSetValue = context.tableViewModel.syncEnable && context.infoModel.setting.syncEnabled;
//            [manager phoneSetSyncWith:phoneSetValue ? @YES : @NO key:@"enable"];
//        }
//        
//    } else if (isScreenSwitch) {
//        [manager appSet:YES
//                    row:context.row
//           autoScreenOn:sender.isOn ? @YES : @NO
//              showStyle:@""
//               callback:^(NSDictionary *param) {
//            [self handleCallbackResult:param context:context];
//        }];
//        
//        // 电话特殊处理
//        if ([self isPhoneTag:sender.tag]) {
//            [manager phoneSetSyncWith:sender.isOn ? @YES : @NO key:@"autoScreenOn"];
//        }
    }
    
    // 5. 刷新UI
    if (context.reloadHandler) {
        context.reloadHandler();
    }
}

- (BOOL)isMainSwitchTag:(WowNotificationSwitchTag)tag {
    return tag == WowNotificationSwitchTagPhone ||
           tag == WowNotificationSwitchTagMessage ||
           tag == WowNotificationSwitchTagSchedule ||
           tag == WowNotificationSwitchTagAlarm;
}

- (BOOL)isScreenSwitchTag:(WowNotificationSwitchTag)tag {
    return tag == WowNotificationSwitchTagPhoneScreen ||
           tag == WowNotificationSwitchTagMessageScreen ||
           tag == WowNotificationSwitchTagScheduleScreen ||
           tag == WowNotificationSwitchTagAlarmScreen;
}

- (BOOL)isPhoneTag:(WowNotificationSwitchTag)tag {
    return tag == WowNotificationSwitchTagPhone ||
           tag == WowNotificationSwitchTagPhoneScreen;
}

- (void)handleCallbackResult:(NSDictionary *)param context:(WowSwitchContext *)context {
    // 处理回调结果
    NSNumber *code = param[@"code"];
    if (code && code.integerValue != 1) {
        NSLog(@"App switch operation failed with code: %@", code);
    }
}

- (NSInteger)priority { return 2; }

- (NSString *)strategyDescription { return @"应用开关策略"; }

@end
